from time import sleep
from config.config import *
import appium.webdriver
from appium.options.android import UiAutomator2Options

from selenium.webdriver.common.by import By


class GetDriver:
    __app_driver = None
    __web_driver = None

    # 获取App Driver
    @classmethod
    def get_app_driver(cls):
        if cls.__app_driver is None:
            options = UiAutomator2Options()
            options.platform_name = platform_name
            options.platform_version = platform_version
            options.device_name = device_name
            options.app_package = app_package
            # options.app_activity = "com.lightcone.prettyo.activity.main.MainActivit"
            options.unicode_keyboard = unicode_keyboard
            options.reset_keyboard = reset_keyboard
            options.no_reset = no_reset

            options.set_capability("autoLaunch", autoLaunch)
            options.set_capability("dontStopAppOnReset", dontStopAppOnReset)

            cls.__app_driver = appium.webdriver.Remote(
                command_executor=command_executor,
                options=options
            )
            print("已连接到正在运行的App")
        return cls.__app_driver

    @classmethod
    def reset_to_main_activity(cls):
        # 跳转回主Activity
        main_activity = "com.lightcone.prettyo.activity.main.MainActivity"
        app_package = "com.accordion.prettyo.cn"
        driver = cls.get_app_driver()
        driver.start_activity(app_package, main_activity)
        print(f"跳转回{main_activity}")
        sleep(2)  # 适当等待界面加载


if __name__ == '__main__':
    driver = GetDriver.get_app_driver()
    ele = driver.find_element(By.ID, "com.accordion.prettyo.cn:id/iv_photo")
    print("获取到元素")
    sleep(3)
    ele.click()
    print("点击到元素")
    driver.terminate_app(app_package)
    driver.activate_app(app_package)
